package com.mdd.admin.controller.competition;

import com.mdd.admin.aop.Log;
import com.mdd.admin.service.ICompetitionApplyService;
import com.mdd.admin.validate.commons.IdValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.competition.CompetitionApplyCreateValidate;
import com.mdd.admin.validate.competition.CompetitionApplyImportValidate;
import com.mdd.admin.validate.competition.CompetitionApplySearchValidate;
import com.mdd.admin.validate.competition.CompetitionApplyUpdateValidate;
import com.mdd.admin.vo.competition.CompetitionApplyDetailVo;
import com.mdd.admin.vo.competition.CompetitionApplyImportVo;
import com.mdd.admin.vo.competition.CompetitionApplyListedVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/adminapi/competition.apply")
@Api(tags = "赛事申请管理")
public class CompetitionApplyController {

    @Resource
    ICompetitionApplyService iCompetitionApplyService;

    @GetMapping("/lists")
    @ApiOperation(value="赛事申请列表")
    public AjaxResult<PageResult<CompetitionApplyListedVo>> list(@Validated PageValidate pageValidate,
                                                     @Validated CompetitionApplySearchValidate searchValidate) {
        PageResult<CompetitionApplyListedVo> list = iCompetitionApplyService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value="赛事申请详情")
    public AjaxResult<CompetitionApplyDetailVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        CompetitionApplyDetailVo detail = iCompetitionApplyService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "赛事申请新增")
    @PostMapping("/add")
    @ApiOperation(value="赛事申请新增")
    public AjaxResult<Object> add(@Validated @RequestBody CompetitionApplyCreateValidate createValidate) {
        iCompetitionApplyService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "赛事申请编辑")
    @PostMapping("/edit")
    @ApiOperation(value="赛事申请编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody CompetitionApplyUpdateValidate updateValidate) {
        iCompetitionApplyService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "赛事申请删除")
    @PostMapping("/del")
    @ApiOperation(value="赛事申请删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iCompetitionApplyService.del(idValidate.getId());
        return AjaxResult.success();
    }

    @Operation(summary = "下载导入模板", description = "下载导入模板")
    @GetMapping(value = "/import/template", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void downloadImportTemplate(HttpServletResponse response) throws IOException {
        iCompetitionApplyService.downloadImportTemplate(response);
    }

    @Operation(summary = "解析导入数据", description = "解析导入数据")
    @PostMapping("/import/parse")
    public AjaxResult parseImport(@RequestParam("file") MultipartFile file) {
        return AjaxResult.success(iCompetitionApplyService.parseImport(file));
    }

    @Operation(summary = "导入数据", description = "导入数据")
    @PostMapping(value = "/import")
    public AjaxResult importUser(@Validated @RequestBody CompetitionApplyImportValidate req) {
        CompetitionApplyImportVo competitionApplyImportVo = iCompetitionApplyService.importUser(req);
        return AjaxResult.success(competitionApplyImportVo);
    }

}
